package _91Annotation.bean;
//实现克隆
public class Person implements Cloneable{
    private String name;
    private String sex;
    private int age;

    private Dog  dog;

    /*
    每个类对象都可以自我克隆,但是Object中clone方法默认是protected,在类外部无法被调用
    子类重写时 只需要修改访问权限为public即可  代码中依然使用 super.clone()
    需要能克隆的类对象还必须实现 Cloneable接口

    一般是浅度克隆，仅克隆该对象自己，对象中如果还有其他对象作为属性 则不会克隆
    深度克隆：不仅克隆对象本身  还克隆其中包含的其他对象属性，修改浅度克隆的代逻辑
     */
    @Override
    public Object clone() throws CloneNotSupportedException {
        //深度克隆 克隆Person的同时再克隆Dog
        Person  other = (Person) super.clone();
        Dog  otherDog = (Dog) this.dog.clone();
        other.setDog(otherDog);//将克隆Dog设置给克隆的Person
        return  other;
    }




    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Person(String name, String sex, int age,Dog dog) {
        System.out.println("Person 构造函数");
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }

}
